from PySide2.QtWidgets import QApplication, QGroupBox, QVBoxLayout, QWidget
from PySide2.QtWebEngineWidgets import QWebEngineView
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile,QUrl
from PySide2.QtWidgets import QApplication,  QWidget
from PySide2.QtGui import QDesktopServices
import sys
import os
class Draw_Charts(QWidget):
    def __init__(self):

        # 从文件加载排序界面UI定义
        sortui_path = QFile("UI/sort.ui")
        sortui_path.open(QFile.ReadOnly)
        sortui_path.close()
        # 创建动态定义，实现前后端分离
        self.uit = QUiLoader().load(sortui_path)
        # 定义按钮功能
        self.uit.pushButton.clicked.connect(self.tomain2)
        # 定义按钮2功能
        self.uit.pushButton_2.clicked.connect(self.update_chart_sort)


        # 从文件加载主界面UI定义
        mainui_path = QFile("UI/mainframe.ui")
        mainui_path.open(QFile.ReadOnly)
        mainui_path.close()
        # 创建动态定义，实现前后端分离
        self.uim = QUiLoader().load(mainui_path)
        # 获取预测界面的comboBox1信息变化情况
        self.uim.comboBox1.currentTextChanged.connect(self.update_chart_main)
        # 获取预测界面的comboBox2信息变化情况
        self.uim.comboBox2.currentTextChanged.connect(self.update_chart_main)
        # 定义转跳predict页面按钮,这里记住不能先加括号，一定要直接引用
        self.uim.pushButton_2.clicked.connect(self.topredict)
        # 定义转跳页面按钮
        self.uim.pushButton_3.clicked.connect(self.tosort)
        # 定义转跳页面按钮
        self.uim.actionopen_in_web.triggered.connect(self.open_GDPweb)


        #加载预测页面的界面定义
        subui_path = QFile("UI/subframe.ui")
        subui_path.open(QFile.ReadOnly)
        subui_path.close()
        # 创建动态定义，实现前后端分离
        self.uis = QUiLoader().load(subui_path)
        #获取预测界面的comboBox1信息变化情况
        self.uis.comboBox1.currentTextChanged.connect(self.update_chart_predict)
        #获取预测界面的comboBox2信息变化情况
        self.uis.comboBox2.currentTextChanged.connect(self.update_chart_predict)
        #创建QWebEngineView部件,作为临时存放图表的地段
        self.web_view = QWebEngineView()
        self.boxlayout = QVBoxLayout()
        # 定义按钮功能
        self.uis.pushButton_2.clicked.connect(self.tomain1)

    #负责更新预测界面的图表信息更新
    def update_chart_predict(self):
        # 获取当前用户点击编号
        tex1 = self.uis.comboBox1.currentText()
        tex2 = self.uis.comboBox2.currentText()

        if tex1 == "中国" and tex2 == "GDP总值预测":
            html_file = os.path.abspath("data/预测/中国GDP总量预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "法国" and tex2 == "GDP总值预测":
            html_file = os.path.abspath("data/预测/法国GDP总量预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "印度" and tex2 == "GDP总值预测":
            html_file = os.path.abspath("data/预测/印度GDP总量预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "美国" and tex2 == "GDP总值预测":
            html_file = os.path.abspath("data/预测/美国GDP总量预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "中国" and tex2 == "GDP各国占比预测":
            html_file = os.path.abspath("data/预测/中国GDP占比预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "法国" and tex2 == "GDP各国占比预测":
            html_file = os.path.abspath("data/预测/法国GDP占比预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "印度" and tex2 == "GDP各国占比预测":
            html_file = os.path.abspath("data/预测/印度GDP占比预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

        if tex1 == "美国" and tex2 == "GDP各国占比预测":
            html_file = os.path.abspath("data/预测/美国GDP占比预测.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uis.groupBox.setLayout(self.boxlayout)

    #负责主界面的信息更新
    def update_chart_main(self):
        #实时获取用户选择
        tex1 = self.uim.comboBox1.currentText()
        tex2 = self.uim.comboBox2.currentText()
        # 对相关信息进行处理
        if tex1 == "中国" and tex2 == "第一产业":
            html_file = os.path.abspath("data/中国/中国GDP-农业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "法国" and tex2 == "第一产业":
            html_file = os.path.abspath("data/法国/法国GDP-农业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "印度" and tex2 == "第一产业":
            html_file = os.path.abspath("data/印度/印度GDP-农业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "美国" and tex2 == "第一产业":
            html_file = os.path.abspath("data/美国/美国GDP-农业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "中国" and tex2 == "第二产业":
            html_file = os.path.abspath("data/中国/中国GDP-工业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "法国" and tex2 == "第二产业":
            html_file = os.path.abspath("data/法国/法国GDP-工业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "印度" and tex2 == "第二产业":
            html_file = os.path.abspath("data/印度/印度GDP-工业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "美国" and tex2 == "第二产业":
            html_file = os.path.abspath("data/美国/美国GDP-工业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "中国" and tex2 == "第三产业":
            html_file = os.path.abspath("data/中国/中国GDP-服务业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "法国" and tex2 == "第三产业":
            html_file = os.path.abspath("data/法国/法国GDP-服务业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "印度" and tex2 == "第三产业":
            html_file = os.path.abspath("data/印度/印度GDP-服务业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "美国" and tex2 == "第三产业":
            html_file = os.path.abspath("data/美国/美国GDP-服务业.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "中国" and tex2 == "总体GDP":
            html_file = os.path.abspath("data/中国/中国GDP总值.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "法国" and tex2 == "总体GDP":
            html_file = os.path.abspath("data/法国/法国GDP-总值.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "印度" and tex2 == "总体GDP":
            html_file = os.path.abspath("data/印度/印度GDP-总值.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)

        if tex1 == "美国" and tex2 == "总体GDP":
            html_file = os.path.abspath("data/美国/美国GDP-总值.html")
            self.web_view.load(QUrl.fromLocalFile(html_file))
            self.boxlayout.addWidget(self.web_view)
            self.uim.groupBox.setLayout(self.boxlayout)
    def update_chart_sort(self):
        html_file = os.path.abspath("data/Flourish template_ Bar chart race.html")
        self.web_view.load(QUrl.fromLocalFile(html_file))
        self.boxlayout.addWidget(self.web_view)
        self.uit.groupBox.setLayout(self.boxlayout)

    def topredict(self):
        self.uim.hide()
        self.uis.show()
    #转跳到主界面
    def tomain1(self):
        self.uis.hide()
        self.uim.show()
    def tomain2(self):
        self.uit.hide()
        self.uim.show()
    def tosort(self):
        self.uim.hide()
        self.uit.show()
    #通过文件菜单连接到网页展示版本
    def open_GDPweb(self):
        url1 = QUrl("http://8.140.199.221/")
        QDesktopServices.openUrl(url1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Draw_Charts()
    ui.uim.show()
    sys.exit(app.exec_())





